//
//  MyApplyCell.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/12/22.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit

class MyApplyCell: UITableViewCell {

    
    @IBOutlet weak var apllyNOLabel: UILabel!
    @IBOutlet weak var careName: UILabel!
    
    @IBOutlet weak var applyDate: UILabel!
    
    @IBOutlet weak var applyMobileLabel: UILabel!
    
    @IBOutlet weak var careMobileLabel: UILabel!
    
    @IBOutlet weak var detailButton: UIButton!
    
    
    @IBOutlet weak var auditDateLabel: UILabel!
    
    @IBOutlet weak var auditResultLabel: UILabel!
    
    @IBOutlet weak var releaseDateLabel: UILabel!
    
    @IBOutlet weak var resultDateLabel: UILabel!
    
    @IBOutlet weak var bottomView: UIView!
    
    var model: MyApplyModel! {
        willSet {
            if (newValue != nil) {
                apllyNOLabel.text = "\(newValue.applyNO)"
                careName.text = "监护人： \(newValue.carePeople)"
                applyDate.text = "申请日期： \(newValue.applyDate)"
                applyMobileLabel.text = "申请人电话： \(newValue.mobileNO)"
                careMobileLabel.text = "监护人电话： \(newValue.carePeopleMobile)"
                
                bottomView.isHidden = !newValue.showDetail
                
                let text = newValue.canModify ? "修改申请" : "查看详情"
                detailButton.setTitle(text, for: UIControlState.normal)
                ////
                if newValue.detailModel == nil{
                    return
                }
                
                auditDateLabel.text = "审核时间： \(newValue.detailModel.trialDate)"
                auditResultLabel.text = "审核结果： \(newValue.detailModel.trialState)"
                releaseDateLabel.text = "公布时间： \(newValue.detailModel.publicityDate)"
                resultDateLabel.text = "结论时间： \(newValue.detailModel.conclusionDate)"
            }
        }
    }
    
    var changeHeightClouse: (() -> (Void))?
    
//    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
//        super.init(style: style, reuseIdentifier: reuseIdentifier)
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        super.init(coder: aDecoder)
//    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        selectionStyle = .none
        detailButton.addTarget(self, action: #selector(showDetailAction), for: UIControlEvents.touchUpInside)
    }

    @objc func showDetailAction() {
        if model != nil {
            
            if model.canModify {
                if let vc = CDUtility.currentViewController() {
                    let applyVC = ApplyMenuViewController()
                    applyVC.oldApplyID = model.applyNO
                    vc.navigationController?.pushViewController(applyVC, animated: true)
                }
                return;
            }
            
            if model.detailModel != nil && model.showDetail{
//                model.showDetail = !model.showDetail
//
//                if let vc = CDUtility.currentViewController() {
//                    let applyVC = ApplyMenuViewController()
//                    applyVC.oldApplyID = model.applyNO
//                    vc.navigationController?.pushViewController(applyVC, animated: true)
//                }
                return
            }
            
            model.getDetailRequest { [weak self] in
                if let strongSelf = self {
                    // 退回时，修改申请
                    strongSelf.model.showDetail = true
                    if strongSelf.changeHeightClouse != nil {
                        strongSelf.changeHeightClouse!()
                    }
                }
            }
        }
    }
    
}
